﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Entity.Models;
using JinfuyuanProject.Repository.Interface;
using JinfuyuanProject.Repository.Repository;
using JinfuyuanProject.Service.Interface;
using MongoDB.Driver;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Linq;

namespace JinfuyuanProject.Service.Service
{

    public class MParturientService: IMParturientService
    {
        private readonly IMParturientRepository __repository;
        private readonly JinfuyuanMongoDbContext _context;
        private readonly IParturientRepository __parepository;

        public MParturientService(IMParturientRepository repository ,JinfuyuanMongoDbContext context,IParturientRepository parturientRepository) {
            __repository = repository;
            _context = context;
            __parepository =parturientRepository;
        }
        #region 基础方法
        public int Delete(MParturient Parturient)
        {
            return __repository.Delete(Parturient);
        }
        public List<MParturient> GetList()
        {
            return __repository.GetList();
        }
        public MParturient GetModel(string name)
        {
            return __repository.GetModel(name);
        }
        public int Insert(MParturient Parturient)
        {
            return __repository.Insert(Parturient);
        }
        public int Update(MParturient Parturient)
        {
            return __repository.Update(Parturient);
        }
        #endregion
        #region 业务方法
        public Hashtable Query(ParturientFilter filter)
        {
            int count;
            Staff staff = (Staff)HttpContext.Current.Session["user"];
            List<MParturient> parturients=new List<MParturient>();
            //管理员
            parturients = GetList();
            foreach (var item in parturients)
            {
                var pa=__parepository.GetModel(item.ParturientName,item.ParturientPhone);
                if (pa != null)
                {
                    item.IsDelete = 1;
                }
            }
            if (staff.RoleId == 2)
            {
                string num = staff.Numbers;
                //业务员工号
             parturients = GetList().Where(x => x.SalesmanNumbers == num).ToList();//两表关联 使用linq  多表复杂SQL 使用QueryBySql
            }
            Hashtable hashtable = new Hashtable();
            hashtable["code"] = 0;
            hashtable["message"] = "success";
            hashtable["data"] = MakeQueryFilter(parturients, filter, out count);
            hashtable["count"] = count;//结束惰性加载
            return hashtable;
        }
        #endregion
        #region 公共方法
        public List<MParturient> MakeQueryFilter(List<MParturient> parturients, ParturientFilter parturientFilter, out int count)
        {
            if (!string.IsNullOrEmpty(parturientFilter.ParturientName))
            {
                parturients = parturients.Where(x => x.ParturientName.Contains(parturientFilter.ParturientName)).ToList();
            }

            if (parturientFilter.is_delete != null)
            {
                parturients = parturients.Where(x => x.IsDelete == parturientFilter.is_delete).ToList();
            }
            if (!string.IsNullOrEmpty(parturientFilter.ParturientPhone))
            {
                parturients = parturients.Where(x => x.ParturientPhone==parturientFilter.ParturientPhone).ToList();
            }
            if (!string.IsNullOrEmpty(parturientFilter.Address))
            {
                parturients = parturients.Where(x => x.Address.Contains(parturientFilter.Address)).ToList();
            }
            //需要过滤后没做分页前的所有数据条数
            count = parturients.Count();
            //做分页
            if (parturientFilter.page != 0 && parturientFilter.limit != 0)
                return parturients.OrderByDescending(x => x.Weight).Skip((parturientFilter.page - 1) * parturientFilter.limit).Take(parturientFilter.limit).ToList();
            else
                return parturients;
        }
        public Hashtable SearchName(string keywords)
        {
            Hashtable hashtable = new Hashtable();
            var list = GetList().Where(x => x.ParturientName.Contains(keywords));
            hashtable["code"] = 0;
            hashtable["msg"] = "success";
            hashtable["data"] = list;
            hashtable["count"] = list.Count();
            return hashtable;
        }


        #endregion
    }
}
